<?php

	session_start();

	header('Content-type: image/png');
	
	/**
	 * array (
	 * 	'text',
	 * 	'font_size'
	 * )
	 */
	$cp_arr = $_SESSION['captcha_config'];
	
	function cp_create_img($text, $font_size)
	{
		$text_arr = str_split($text);
		
		$font_file = realpath("captcha.ttf");
		
		$img_width = (count($text_arr) * $font_size) + (count($text_arr) * 1) + 20;
		
		$_tmp = imageCreateTrueColor($img_width, $font_size + 20);
		
		$white = imageColorAllocate($_tmp, 255, 255, 255);
		
		foreach($text_arr as $i => $value)
		{
			$padding_left = ($i * $font_size) + ($i * 1);
			
			$padding_top = $font_size;
			
			imageTtfText($_tmp, $font_size, rand(-18, 18), $padding_left + 10, $padding_top + 10, $white, $font_file, strtolower($value));
			
			$rand_color = imageColorAllocate($_tmp, rand(0,255), rand(0,255), rand(0,255));
			
			imageFilledRectangle($_tmp , $padding_left , 0 , $padding_left , ($font_size+$padding_left) , $rand_color);
			
			
		}
		//imageTtfText($_tmp, $font_size, 0, 0, $font_size, $white, $font_file, '5Ch4gJk');
		
		imagePng($_tmp);
		
		imageDestroy($_tmp);	
	}
	cp_create_img($cp_arr['text'], $cp_arr['font_size']);